import base64
 import datetime
 import hashlib
 import re
 
 # 替换时的字符串常量
 zm_constant = "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"
 
 
 # 计算给定字符串的MD5值
 def getMd5(s):
     md52 = hashlib.md5(s.encode("utf-8")).digest()
     result = ""
     for b in md52:
         i = b & 255
         result += '{:02x}'.format(i)
     return result
 
 
 # 用指定的des_str代替str4的index索引处的字母
 def replaceSomeWhere(str4, des_str, index):
     if index != 1:
         return str4[0:index - 1] + des_str +str4[index:]
     else:
         return des_str + str4[index:]
 
 
 # 获取每天的Token值
 def getToken(str1, str2):
     result = ""
     md52_str = getMd5(str1 + "Tide" + str2)
     result += md52_str[0:2]
     result += md52_str[4:8]
     result += md52_str[14:15]
     result += md52_str[13:14]
     result += md52_str[12:13]
     result += md52_str[25:29]
     result += md52_str[30:31]
     result += md52_str[29:30]
     result += md52_str[28:29]
     result += md52_str[27:28]
     result += md52_str[26:27]
     str3 = int(datetime.datetime.now().timestamp())
     md53_str = getMd5("tide" + str(str3) +result)
     replace = base64.b64encode(("t=" +str(str3) + "&c=" + str1 + "&a=" + str2 +"&v=" +
                                 md53_str[4:5] +md53_str[16:17] + md53_str[31:32] +md53_str[17:18] + md53_str[
                                                                                                       8:9]).encode(
         "utf-8")).decode().replace(
         "=", "|")
     dig_list = re.findall("[1-9]", replace)
     dig_list = list(set(dig_list))
     str4 = replace
     for dig in dig_list:
         dig = int(dig)
         if dig % 2 == 0:
             sub1 = replace[dig - 1:dig]
             if zm_constant.index(sub1) != -1:
                 indexOf = (zm_constant.index(sub1) + (dig * dig)) % 52
                 str4 = replaceSomeWhere(str4, zm_constant[indexOf:indexOf + 1], dig)
         else:
             sub2 = replace[dig - 1:dig]
             if zm_constant.index(sub2) != -1:
                 indexOf = (zm_constant.index(sub2) + dig) % 52
                 str4 = replaceSomeWhere(str4, zm_constant[indexOf:indexOf + 1], dig)
     return str4